package com.zzptc.kaoshi;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.zzptc.kaoshi.adapter.Xianshi_adapter;
import com.zzptc.kaoshi.bean.Info;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    String srt;
    TextView tv_xianshi;
    RecyclerView recyclerView;
    List<Info> list = new ArrayList<Info>();
    Xianshi_adapter xianshi_adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_xianshi = (TextView) findViewById(R.id.tv_xianshi);
        recyclerView = (RecyclerView) findViewById(R.id.rv_xianshi);
        new Thread(new Runnable() {
            @Override
            public void run() {

                new AsyncTask<String, View, String>() {
                    @Override
                    protected String doInBackground(String... strings) {

                        try {
                            URL url = new URL("http://192.168.1.111:8080/mobilebaidu/versionUpdateServlet");
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.setRequestMethod("GET");
                            if (connection.getResponseCode() == 200){
                                InputStream is = connection.getInputStream();
                                BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
                                srt = bfr.readLine();
                                is.close();
                                bfr.close();
                            }

                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return srt;
                    }

                    @Override
                    protected void onPostExecute(String s) {
                        super.onPostExecute(s);
                        tv_xianshi.setText(s);

                        try {
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i = 0; i < jsonArray.length(); i++){
                                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                int stu_id = jsonObject.optInt("stu_id");
                                String stu_name = jsonObject.optString("stu_name");
                                String sut_pwd = jsonObject.optString("sut_pwd");

                                Info info = new Info();
                                info.setStu_id(stu_id);
                                info.setStu_name(stu_name);
                                info.setSut_pwd(sut_pwd);

                                list.add(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
                        recyclerView.setLayoutManager(linearLayoutManager);

                        xianshi_adapter = new Xianshi_adapter(list);
                        recyclerView.setAdapter(xianshi_adapter);

                    }
                }.execute();
            }
        }).start();



    }
}
